﻿package net.wangit.framework.config;

import net.wangit.Path;
import net.wangit.dev.LabelRule;
import net.wangit.util.FileUtil;
import net.wangit.util.XMLUtil;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.dom4j.Document;
import org.dom4j.Element;


/**
 * 组件设置
 * 
 * @author watson
 * 
 */
public class ConsoleSettings {
	private static Log log = LogFactory.getLog(ConsoleSettings.class);
	public final static String FILE_NAME = "console.xml";
	private static ConsoleSettings instance;

	private static boolean isAutoSaveVersion; // 自动保存版本
	private static String componentLabelRule; // 组件树标签显示方式:1|编号;2|名称;12|编号名称;21|名称编号

	private ConsoleSettings() {
	}

	public static ConsoleSettings instance() {
		if (instance == null) {
			instance = new ConsoleSettings();
			instance.loadDefault();
		}
		return instance;
	}

	private void loadDefault() {
		isAutoSaveVersion = true;
		componentLabelRule = LabelRule.IDNAME;
	}

	public void load() {
		String path = Path.getConfigPath(ConsoleSettings.FILE_NAME);
		if (FileUtil.exists(path)) {
			Document document = XMLUtil.parseByPath(path);
			Element root = document.getRootElement();
			isAutoSaveVersion = XMLUtil.getTextBoolean(root,
					"isAutoSaveVersion", true);
			componentLabelRule = XMLUtil.getTextString(root,
					"componentLabelRule", LabelRule.IDNAME);
			log.info("load console settings successfully.");
		}else{
			this.loadDefault();
		}
	}

	public String getComponentLabelRule() {
		return componentLabelRule;
	}

	public boolean isAutoSaveVersion() {
		return isAutoSaveVersion;
	}
}
